package com.example.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.example.mybatisplus.service.impl.SingleTimeServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.SingleTimeService;
import com.example.mybatisplus.model.domain.SingleTime;

import java.util.*;

import static com.example.mybatisplus.common.Constants.PERIOD;
import static com.example.mybatisplus.common.Constants.WEEKDAY;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2022-05-31
 * @version v1.0
 */
@RestController
@RequestMapping("/api/singleTime")
public class SingleTimeController {

    private final Logger logger = LoggerFactory.getLogger( SingleTimeController.class );

    @Autowired
    private SingleTimeService singleTimeService;

    @GetMapping("/getIdByWeekdayAndTime/")
    public JsonResponse getIdByWeekdayAndTime(@Param("weekday") String weekday,@Param("time") String time){
        return JsonResponse.success(singleTimeService.getIdByTime(time,weekday)).setMessage("查询时间段id成功");
    }

    @GetMapping("/")
    public JsonResponse deleteByWeekdayAndTime(@Param("Weekday") String Weekday,
                                                @Param("Time") String Time) {
        if(singleTimeService.deleteByWeekdayAndTime(Weekday,Time)){
            return JsonResponse.success().setMessage("删除成功");
        }
        return JsonResponse.failure("删除失败");
    }

    @GetMapping("/dict/weekday")
    public JsonResponse week(){
        return JsonResponse.success(singleTimeService.getWeekday(),"查询工作日成功");
    }

    @GetMapping("/dict/time/{weekday}")
    public JsonResponse time(@PathVariable("weekday") String weekday){
        List<Object> result = singleTimeService.getTimeByWeekday(weekday);
        List<Object> finalResult = new ArrayList<>();
        for(Object o : result){
            Map<String,Object> map = new HashMap<>();
            map.put("time",o);
            finalResult.add(map);
        }
        return JsonResponse.success(result,"查询时间段成功").addOtherData("result",finalResult);
    }

    @GetMapping("/dict")
    public JsonResponse dict(){
        List<SingleTime> singleTimes = singleTimeService.list();
        System.out.println(singleTimes);
        Map<String,List<String>> result = new HashMap<>();
        for(SingleTime x : singleTimes){
            if(result.containsKey(x.getWeakDay())){
                List<String> temp = result.get(x.getWeakDay());
                temp.add(x.getTimeDay());
                result.remove(x.getWeakDay());
                result.put(x.getWeakDay(),temp);
            } else {
                List<String> temp = new ArrayList<>();
                temp.add(x.getTimeDay());
                result.put(x.getWeakDay(),temp);
            }
        }
        Set<String> key = result.keySet();
        List<Object> finalResult = new ArrayList<>();
        for(String x : key){
            Map<Object,Object> temp = new HashMap<>();
            temp.put("Week",x);
            temp.put("Time",result.get(x));
            finalResult.add(temp);
        }
        return JsonResponse.success(finalResult,"查询时间段成功！");
    }

    @GetMapping("")
    public JsonResponse show(){
        return JsonResponse.success(singleTimeService.list(),"谢谢你");
    }

    @PostMapping("/delete")
    public JsonResponse batchDelete(@RequestBody List<SingleTime> singleTimes){
        if(singleTimeService.removeByIds(singleTimes)){
            return JsonResponse.success().setMessage("删除成功");
        }
        return JsonResponse.failure("删除失败");
    }

    @GetMapping("/init")
    public JsonResponse initTime(){
        List<SingleTime> singleTimes = new ArrayList<>();
        for (int i = 0; i < WEEKDAY.length; i++) {
            for (int j = 0; j < PERIOD.length; j++) {
                SingleTime singleTime = new SingleTime();
                singleTime.setTimeDay(PERIOD[j]);
                singleTime.setWeakDay(WEEKDAY[i]);
                singleTimes.add(singleTime);
            }
        }
        if(singleTimeService.saveBatch(singleTimes)){
            return JsonResponse.success().setMessage("插入成功");
        }
        return JsonResponse.failure("谢谢你插入失败");
    }
    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        SingleTime  singleTime =  singleTimeService.getById(id);
        return JsonResponse.success(singleTime);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        singleTimeService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateSingleTime(SingleTime  singleTime) throws Exception {
        singleTimeService.updateById(singleTime);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建SingleTime
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(@RequestBody SingleTime  singleTime) throws Exception {
        singleTimeService.save(singleTime);
        return JsonResponse.success(null).setMessage("添加时间段成功");
    }
}

